package com.bRoom.service.impl;

import com.bRoom.common.ServerResponse;
import com.bRoom.dao.CarMapper;
import com.bRoom.dao.RoomMapper;
import com.bRoom.pojo.Car;
import com.bRoom.pojo.Room;
import com.bRoom.pojo.User;
import com.bRoom.service.ICarService;
import com.bRoom.util.DateTimeUtil;
import com.bRoom.vo.CarListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service("iCarService")
public class CarServiceImpl implements ICarService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private RoomMapper roomMapper;

    public ServerResponse<Car> addCar (Car car,Integer userId){
            if (car!=null){
                car.setUserId(userId);
                String checkINtime= DateTimeUtil.DatetoStr(car.getCheckintime());
                car.setCheckintime(DateTimeUtil.StringToDate(checkINtime));

                String checkOUTtime= DateTimeUtil.DatetoStr(car.getCheckouttime());
                car.setCheckintime(DateTimeUtil.StringToDate(checkOUTtime));
                 int addCarCount=carMapper.insert(car);
                 if (addCarCount>0){
                      return ServerResponse.createBySuccessMsg("添加到订房车成功");
                 }
                 return ServerResponse.createByErrorMsg("添加失败");
            }
            return ServerResponse.createByErrorMsg("参数错误");
    }

    public  ServerResponse<Car> updateCar(Car car,Integer userId){
           if (userId==null){
                        return ServerResponse.createByErrorMsg("用户差数错误，请重新登录");
           }
           Car updateCar=new Car();
           updateCar.setUserId(userId);
           String checkINtime= DateTimeUtil.DatetoStr(car.getCheckintime());
            car.setCheckintime(DateTimeUtil.StringToDate(checkINtime));

          String checkOUTtime= DateTimeUtil.DatetoStr(car.getCheckouttime());
           updateCar.setCheckintime(DateTimeUtil.StringToDate(checkINtime));
           updateCar.setCheckouttime(DateTimeUtil.StringToDate(checkOUTtime));
           updateCar.setRoomId(car.getRoomId());
           String updateTime=DateTimeUtil.DatetoStr(new Date());
           updateCar.setUpdatetome(DateTimeUtil.StringToDate(updateTime));
           int updateCount=carMapper.updateByPrimaryKeySelective(updateCar);
           if (updateCount>0){
                return ServerResponse.createBySuccessMsg("修改成功");
           }
           return ServerResponse.createByErrorMsg("修改失败");
    }

    public ServerResponse<PageInfo> carList(User user,int psgrNum,int pageSize){
        PageHelper.startPage(psgrNum,pageSize);
        List<Car> carList=carMapper.selectCarList(user.getUserId());
        List<CarListVo> carListVoList= Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(carList)){
                    for (Car carItem :carList){
                         CarListVo carListVo=assambleCarListVo(carItem,user.getUserId(),user.getUserName());
                                        carListVoList.add(carListVo);
                    }
                    PageInfo result=new PageInfo(carList);
                    result.setList(carListVoList);
                    return ServerResponse.createBySuccessData(result);
        }
      return ServerResponse.createByErrorMsg("找不到相关信息");
    }

    private CarListVo assambleCarListVo(Car car,Integer userId,String userName){
              CarListVo carListVo=new CarListVo();
              Room room=roomMapper.roomDetail(car.getRoomId());
              if (room==null){
                   return null;
              }
              carListVo.setCartId(car.getCartId());
              carListVo.setRoomId(car.getRoomId());
              carListVo.setRoomName(room.getRoomName());
              carListVo.setUserName(userName);
              carListVo.setUserId(userId);
              carListVo.setCheckINtime(DateTimeUtil.DatetoStr(car.getCheckintime()));
              carListVo.setCheckOUTtime(DateTimeUtil.DatetoStr(car.getCheckouttime()));
              return carListVo;
    }
}
